package group

import (
	"context"
	"fmt"

	"go-admin/base/internal/svc"
	"go-admin/base/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ListLogic) List() (resp *types.GroupRespDTOS, err error) {
	groups, err := l.svcCtx.GroupModel.FindAll(l.ctx)
	if err != nil {
		_ = fmt.Errorf(err.Error())
		return nil, err
	}
	var groupRespDTOs []types.GroupRespDTO
	for _, group := range groups {
		groupRespDTOs = append(groupRespDTOs, types.GroupRespDTO{
			Id:          group.Id,
			Name:        group.Name.String,
			NickName:    group.NickName.String,
			Description: group.Description.String,
		})
	}
	return &types.GroupRespDTOS{
		Data: groupRespDTOs,
		Result: types.Result{
			Code: 200,
		},
	}, nil
}
